<?php
/**
 * Nordic Repository
 * 
 * LICENSE
 * 
 * The new BSD license is applied on this source-file. For further
 * information please visit http://license.nordic-dev.de/newbsd.txt
 * or send an email to andre.moelle@gmail.com.
 */

/**
 * This class represents the conditional comments for the IE.
 * 
 * @category   Nordic
 * @package    Nordic_Pagebuilder
 * @copyright  2007 Nordic Development
 * @license    http://license.nordic-dev.de/newbsd.txt (New-BSD license)
 * @author     Andre Moelle <andre.moelle@gmail.com>
 * @version    $Id: ConditionalComment.php 3 2007-07-08 09:21:42Z andre.moelle $
 */
class Nordic_Pagebuilder_Decorator_ConditionalComment extends Nordic_Pagebuilder_Decorator
{
	/**
	 * Comparator which is used in the conditional-comment.
	 * 
	 * @var string
	 */
	private $_comparator;
	
	/**
	 * Transformations used by the conditional comments.
	 * 
	 * @var array
	 */
	private $_transform = array(
		'>=' => 'gte',
		'=>' => 'gte',
		'>' => 'gt',
		'=' => '',
		'==' => '',
		'<=' => 'lte',
		'=<' => 'lte',
		'<' => 'lt',
		'!' => '!',
		'!=' => '!',
		'gte' => 'gte',
		'gt' => 'gt',
		'lte' => 'lte',
		'lt' => 'lt',
		'' => ''
	);
	
	/**
	 * Version of the IE.
	 * 
	 * @var string
	 */
	private $_version;
	
	/**
	 * Constructor.
	 * 
	 * Saves and transforms the given input.
	 * 
	 * @param string $version version of the IE
	 * @param string $comparator comparator
	 * @return void
	 */
	public function __construct (array $args = array())
	{
		$this->setComparator(gv($args['comparator']));
		$this->setVersion(gv($args['version']));
	}
	
	/**
	 * Comments the content out.
	 * 
	 * @see Nordic_Pagebuilder_Decorator::__toString()
	 */
	public function __toString ()
	{
		$result = "<!--[if " . $this->getComparator() .  "IE" . $this->getVersion() . "]>\n";
		
		if($obj = $this->getDecorated())
		{
			$result .= (string)$obj;
		}
		
		$result .= "\n<![endif]-->\n";
		
		return $result;
	}
	
	/**
	 * Transforms a comparator.
	 * 
	 * Returns the comparator which is usable by the conditonal
	 * comments. If the comparator is invalid, an exception called
	 * "Nordic_Pagebuilder_Exception" will be thrown.
	 * 
	 * @throws Nordic_Pagebuilder_Exception
	 * @param string $comparator comparator to transform
	 * @return string
	 */
	private function _transform ($comparator)
	{
		$comparator = strtolower($comparator);
		
		if(!in_array($comparator, array_keys($this->_transform)))
		{
			throw new Nordic_Pagebuilder_Exception(
				'Comparator "' . $comparator . '" does not exist.'
			);
		}
		
		return $this->_transform[$comparator];
	}
	
	/**
	 * Returns the comparator.
	 * 
	 * On every comparator which is not "!" a space is appended.
	 * 
	 * @param void
	 * @return string
	 */
	public function getComparator ()
	{
		if($this->_comparator == '!')
		{
			return '!';
		}
		
		return $this->_comparator . ' ';
	}
	
	/**
	 * Returns the version.
	 * 
	 * @param void
	 * @return string
	 */
	public function getVersion ()
	{
		if($this->_version == '')
		{
			return '';
		}
		
		return ' ' . $this->_version;
	}
	
	/**
	 * Transforms the comparator and sets it.
	 * 
	 * @throws Nordic_Pagebuilder_Exception
	 * @param string $comparator new comparator
	 * @return void
	 */
	public function setComparator ($comparator)
	{
		$this->_comparator = $this->_transform($comparator);
	}
	
	/**
	 * Sets a new version.
	 * 
	 * @param string $version new version
	 * @return void
	 */
	public function setVersion ($version)
	{
		$this->_version = $version;
	}
}
?>